<?php



namespace App\Http\Requests\ApiV2;

use Illuminate\Support\Facades\Auth;

class CouponRequest extends BaseRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'code' => 'required',
            'title' => 'required',
            'coupon_count' => 'required',
            'type' => 'required',
        ];

        $this->isMethod('post') && $rules['code'] .= '|unique:coupons';

        return $rules;
    }

    public function messages()
    {
        return [
            'code.required' => __('请输入优惠码'),
            'code.unique' => __('优惠码已存在'),
            'type.required' => __('请输入优惠券类型'),
        ];
    }

    public function filldata()
    {
        $data = [
            'user_id' => Auth::id(),
            'title' => (string)$this->post('title', ''),
            'reduce_count' => (int)$this->post('reduce_count', ''),
            'discount' => (string)$this->post('discount', 10),
            'expired_days' => (int)$this->post('expired_days', 0),
            'start_time' => $this->post('start_time', null),
            'end_time' => $this->post('start_time', null),
            'type' => $this->post('type', null),
            'minimum_purchase' => $this->post('minimum_purchase', 0),
            'coupon_count' => (int)$this->post('coupon_count', 0),
        ];
        $this->isMethod('post') && $data['code'] = $this->input('code');
        return $data;
    }
}
